package co.yixiang.yshop.module.member.controller.admin.userlevelconfig;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.member.controller.admin.userlevelconfig.vo.*;
import co.yixiang.yshop.module.member.convert.userlevelconfig.UserLevelConfigConvert;
import co.yixiang.yshop.module.member.dal.dataobject.userlevelconfig.UserLevelConfigDO;
import co.yixiang.yshop.module.member.service.userlevelconfig.UserLevelConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 会员等级配置")
@RestController
@RequestMapping("/member/user-level-config")
@Validated
public class UserLevelConfigController {

    @Resource
    private UserLevelConfigService userLevelConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建会员等级配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:create')")
    public CommonResult<Long> createUserLevelConfig(@Valid @RequestBody UserLevelConfigCreateReqVO createReqVO) {
        return success(userLevelConfigService.createUserLevelConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会员等级配置")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:update')")
    public CommonResult<Boolean> updateUserLevelConfig(@Valid @RequestBody UserLevelConfigUpdateReqVO updateReqVO) {
        userLevelConfigService.updateUserLevelConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会员等级配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-level-config:delete')")
    public CommonResult<Boolean> deleteUserLevelConfig(@RequestParam("id") Long id) {
        userLevelConfigService.deleteUserLevelConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会员等级配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:query')")
    public CommonResult<UserLevelConfigRespVO> getUserLevelConfig(@RequestParam("id") Long id) {
        UserLevelConfigRespVO vo = userLevelConfigService.getUserLevelConfigVO(id);
        return success(vo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得会员等级配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:query')")
    public CommonResult<List<UserLevelConfigRespVO>> getUserLevelConfigList(@RequestParam("ids") Collection<Long> ids) {
        List<UserLevelConfigDO> list = userLevelConfigService.getUserLevelConfigList(ids);
        return success(UserLevelConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/listAll")
    @Operation(summary = "获得全部会员等级配置列表")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:query')")
    public CommonResult<List<UserLevelConfigRespVO>> listAll() {
        List<UserLevelConfigDO> list = userLevelConfigService.getUserLevelConfigListAll();
        return success(UserLevelConfigConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会员等级配置分页")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:query')")
    public CommonResult<PageResult<UserLevelConfigRespVO>> getUserLevelConfigPage(@Valid UserLevelConfigPageReqVO pageVO) {
        PageResult<UserLevelConfigDO> pageResult = userLevelConfigService.getUserLevelConfigPage(pageVO);
        return success(UserLevelConfigConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会员等级配置 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-level-config:export')")
    @OperateLog(type = EXPORT)
    public void exportUserLevelConfigExcel(@Valid UserLevelConfigExportReqVO exportReqVO,
                                           HttpServletResponse response) throws IOException {
        List<UserLevelConfigDO> list = userLevelConfigService.getUserLevelConfigList(exportReqVO);
        // 导出 Excel
        List<UserLevelConfigExcelVO> datas = UserLevelConfigConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "会员等级配置.xls", "数据", UserLevelConfigExcelVO.class, datas);
    }

}
